from manim import *

class GeometryTeaching(Scene):
    def construct(self):
        # ========== 1. 创建两个点A、B在屏幕中间 ==========
        
        # 创建点A和点B（固定位置，不移动）
        point_A = Dot(ORIGIN + LEFT * 1, color=BLUE, radius=0.12)
        point_B = Dot(ORIGIN + RIGHT * 1, color=BLUE, radius=0.12)
        
        # 添加点的标签
        label_A = MathTex("A", font_size=36).next_to(point_A, DOWN)
        label_B = MathTex("B", font_size=36).next_to(point_B, DOWN)
        
        # 显示两个点
        self.play(Create(point_A), Create(point_B))
        self.play(Write(label_A), Write(label_B))
        self.wait(1)
        
        # 添加教学文字："两点确定一条直线"
        teaching_text1 = Text("两点确定一条直线", font_size=24, color=YELLOW).to_edge(UP)
        self.play(Write(teaching_text1))
        self.wait(1)
        
        # ========== 2. 连成线段并移动 ==========
        
        # 创建连接A、B的线段（注意：A、B点不移动，只有线段移动）
        line_AB = Line(point_A.get_center(), point_B.get_center(), color=GREEN, stroke_width=4)
        
        # 显示线段
        self.play(Create(line_AB))
        self.wait(1)
        
        # 线段向左平移（A、B点保持不动）
        self.play(line_AB.animate.shift(LEFT * 3), run_time=2)
        self.wait(1)
        
        # 线段旋转到垂直位置
        self.play(Rotate(line_AB, PI/2), run_time=2)
        self.wait(1)
        
        # ========== 3. 创建第三个点C并形成三角形 ==========
        
        # 淡出教学文字1
        self.play(FadeOut(teaching_text1))
        
        # 添加新的教学文字
        teaching_text2 = Text("三个不共线的点确定一个三角形", font_size=24, color=YELLOW).to_edge(UP)
        self.play(Write(teaching_text2))
        
        # 获取线段当前的位置，在右侧3个单位处创建点C
        line_center = line_AB.get_center()
        point_C_pos = line_center + RIGHT * 3
        point_C = Dot(point_C_pos, color=RED, radius=0.12)
        label_C = MathTex("C", font_size=36).next_to(point_C, DOWN)
        
        # 显示点C
        self.play(Create(point_C), Write(label_C))
        self.wait(1)
        
        # 获取三个点的位置，创建三角形Mobject
        vertex_A_pos = line_AB.get_start()
        vertex_B_pos = line_AB.get_end()
        vertex_C_pos = point_C.get_center()
        
        # 创建三角形Mobject（不是线段的组合，而是一个整体）
        triangle_ABC = Polygon(
            vertex_A_pos, vertex_B_pos, vertex_C_pos,
            color=BLUE,
            fill_opacity=0.2,
            stroke_width=4
        )
        
        # 先移除原来的线段，然后显示三角形
        self.play(FadeOut(line_AB))
        self.play(Create(triangle_ABC))
        self.wait(1)
        
        # 添加三角形的顶点标签（重新定位）
        triangle_label_A = MathTex("A", font_size=36).next_to(vertex_A_pos, LEFT)
        triangle_label_B = MathTex("B", font_size=36).next_to(vertex_B_pos, RIGHT)
        triangle_label_C = MathTex("C", font_size=36).next_to(vertex_C_pos, DOWN)
        
        # 移除原来的点和标签，添加三角形的标签
        self.play(
            FadeOut(point_A), FadeOut(point_B), FadeOut(point_C),
            FadeOut(label_A), FadeOut(label_B), FadeOut(label_C)
        )
        self.play(
            Write(triangle_label_A), 
            Write(triangle_label_B), 
            Write(triangle_label_C)
        )
        self.wait(1)
        
        # ========== 4. 复制三角形并移动到右侧 ==========
        
        # 淡出教学文字2
        self.play(FadeOut(teaching_text2))
        
        # 添加全等概念的教学文字
        teaching_text3 = Text("全等三角形：形状和大小完全相同", font_size=24, color=YELLOW).to_edge(UP)
        self.play(Write(teaching_text3))
        
        # 复制三角形
        triangle_DEF = triangle_ABC.copy()
        
        # 创建新的标签D、E、F
        # 需要计算新三角形的顶点位置
        triangle_label_D = MathTex("D", font_size=36)
        triangle_label_E = MathTex("E", font_size=36)
        triangle_label_F = MathTex("F", font_size=36)
        
        # 将复制的三角形移动到屏幕右侧
        self.play(triangle_DEF.animate.shift(RIGHT * 6), run_time=2)
        
        # 显示复制的三角形
        self.play(Create(triangle_DEF))
        
        # 更新标签位置（根据新三角形的顶点位置）
        new_vertices = triangle_DEF.get_vertices()
        triangle_label_D.next_to(new_vertices[0], LEFT)
        triangle_label_E.next_to(new_vertices[1], RIGHT)
        triangle_label_F.next_to(new_vertices[2], DOWN)
        
        self.play(
            Write(triangle_label_D),
            Write(triangle_label_E),
            Write(triangle_label_F)
        )
        self.wait(1)
        
        # ========== 5. 显示全等符号 ==========
        
        # 创建全等符号 ≅
        congruent_symbol = MathTex(r"\triangle ABC \cong \triangle DEF", font_size=48, color=GREEN)
        congruent_symbol.move_to(ORIGIN + DOWN * 2)
        
        # 显示全等符号
        self.play(Write(congruent_symbol))
        self.wait(2)
        
        # ========== 6. 最终展示和总结 ==========
        
        # 高亮显示两个三角形，表示它们是全等的
        self.play(
            triangle_ABC.animate.set_fill(BLUE, opacity=0.4),
            triangle_DEF.animate.set_fill(BLUE, opacity=0.4),
            run_time=1
        )
        self.wait(1)
        
        # 让三角形闪烁几次，强调全等关系
        for _ in range(3):
            self.play(
                triangle_ABC.animate.set_stroke(YELLOW, width=6),
                triangle_DEF.animate.set_stroke(YELLOW, width=6),
                run_time=0.3
            )
            self.play(
                triangle_ABC.animate.set_stroke(BLUE, width=4),
                triangle_DEF.animate.set_stroke(BLUE, width=4),
                run_time=0.3
            )
        
        # 最终静止展示
        self.wait(3)
        
        # 可选：淡出所有元素
        self.play(
            FadeOut(triangle_ABC), FadeOut(triangle_DEF),
            FadeOut(triangle_label_A), FadeOut(triangle_label_B), FadeOut(triangle_label_C),
            FadeOut(triangle_label_D), FadeOut(triangle_label_E), FadeOut(triangle_label_F),
            FadeOut(congruent_symbol), FadeOut(teaching_text3)
        )
        self.wait(1)